<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: LJQ <liujiqi@etrol.com> 
// +----------------------------------------------------------------------

namespace app\plan\controller;

use app\common\controller\Admin;
use app\plan\controller\api\Plan as PlanApi;
use app\plan\controller\api\Script as ScriptApi;
use app\admin\controller\ajax\Library as Ajax;


/**
 * 企业预案管理
 * @package app\plan
 */
class company  extends Admin

{
    
    protected $api;
    
	public function _initialize()
	{
		parent::_initialize();
		$this->api = new PlanApi();
            $this->ajax= new Ajax();
            $this->script=new ScriptApi();
	}
    /**
     * 首页
     * 
     */
   	public function index() {
       
   	  if(IS_AJAX){
   	  	$planList = $this->api->planList($this->scope);
          
   	  	return $planList;
   	  }

      $enterpriseList = model('basic/Enterprise')->getSelectList($this->scope);
      $this->assign('enterpriseList',$enterpriseList);
      
      $type=$this->ajax->getplanType()->getData();
      $this->assign('type',$type);

      return $this->fetch();
 	}

 	/**
 	 * 企业预案搜索
 	 */
 	public function search($typeId,$unitId) {
           
 		if(IS_AJAX){
                  
			$planList = $this->api->search(input('typeId'),input('unitId'));
 			return $planList;
 		}
 	}
    /**
     * 添加企业预案
     * 
     */
    public function add() {
   		 if(IS_POST){
   		 	$data = input('post.');  
   		 	//有效性
                   if(isset($data['isValid'])){
                     $data['isValid'] = 1;
                     }else{
                       $data['isValid'] = 0;
                     }
                     $data['applyTime']=time();
                  return $this->api->add($data);
   		 }
      $enterpriseList = model('basic/Enterprise')->getSelectList($this->scope);
      $this->assign('enterpriseList',$enterpriseList);
      $type=$this->ajax->getplanType()->getData();
      $this->assign('type',$type);
          
           return $this->fetch();    
     }
 
    /**
     * 修改企业预案
     * 
     */
    public function edit($planId = null) {
      
    	  if(IS_POST){
    	  	$data = input('post.');
                     if(isset($data['isValid'])){
                     $data['isValid'] = 1;
                     }else{
                       $data['isValid'] = 0;
                     }
    	  	return $this->api->edit($data,$planId);
    	  }
     	  $planList = $this->api->planList($this->scope,$planId)->getData();
        $enterpriseList = model('basic/Enterprise')->getSelectList($this->scope);
        $type=$this->ajax->getplanType()->getData();
        $this->assign('enterpriseList',$enterpriseList);
        $this->assign('type',$type);
     	  $this->assign('info',$planList['data'][0]);
        return $this->fetch();      
    }
    
    /**
     * 删除企业预案
     */
    public function del($planId = null) {
    	if(IS_AJAX){
    		return $this->api->del($planId);
    	}
    }


     /**
     * 企业预案步骤列表
     */
    public function scriptList($planId = null) {
        if(IS_AJAX){
        $scriptList = $this->script->scriptList($planId);
         return $scriptList;
      }
      $this->assign('planId',$planId);
      return $this->fetch();
     }

          /**
     * 企业预案步骤列表
     */
    public function scriptAdd($planId = null) {

      if(IS_POST){
          	$data = input('post.');  
            $data['reservePlanId']=$planId;
            return $this->script->add($data);
      }
      $enterpriseList = model('basic/Enterprise')->column('fullName','id');
      $type = $this->ajax->getScriptType()->getData();
      $this->assign('planId',$planId);
      $unitId = model('Plan')->where('id',$planId)->value('unitId');
      $this->assign('enterpriseId',$unitId);
      $this->assign('enterpriseList',$enterpriseList);
      $this->assign('type',$type);
     return $this->fetch();    
     }


    /**
     * 修改企业预案步骤
     * 
     */
    public function scriptedit($planId = null,$scriptId = null) {
      
        if(IS_POST){
          $data = input('post.');
          $data['reservePlanId']=$planId;
          return $this->script->edit($data,$scriptId);
        }

        $scriptList = $this->script->scriptList($planId,$scriptId)->getData();

        $enterpriseList = model('basic/Enterprise')->column('fullName','id');
        $type=$this->ajax->getScriptType()->getData();
         $this->assign('enterpriseList',$enterpriseList);
         $this->assign('type',$type);
         $this->assign('info',$scriptList['data'][0]);
         $this->assign('planId',$planId);
         return $this->fetch();      
    }




    /**
     * 删除企业预案步骤
     */
    public function scriptdel($scriptId = null) {
      if(IS_AJAX){

        return $this->script->del($scriptId);
      }
    }


    /**
     * 申请审核
     * @param string $id
     */
    public function audit($id = null){
    	return $this->api->audit($id);
    }

}



